package elijah.base.elijahbasedemo;

import android.animation.ValueAnimator;
import android.graphics.PixelFormat;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.card.MaterialCardView;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import elijah.base.activity.BaseActivity;
import elijah.base.adapter.BaseAdapter;
import elijah.base.adapter.BaseSimpleAdapter;
import elijah.base.databinding.ItemTextBinding;
import elijah.base.dialog.BaseItemCreateView;
import elijah.base.dialog.BasePdaItemSingleSelectView;
import elijah.base.elijahbasedemo.bean.OrderInfo;
import elijah.base.elijahbasedemo.databinding.ActivityMainBinding;
import elijah.base.elijahbasedemo.databinding.ItemOrderInfoBinding;
import elijah.base.net.BaseResult;
import elijah.base.result.ListResult;
import elijah.base.thread.Threads;
import elijah.base.utils.Toasts;
import elijah.base.widget.ScanWidgetView;
import io.reactivex.Observable;
import io.reactivex.Single;


public class MainActivity extends BaseActivity<ActivityMainBinding> {


    private Fragment demoFragment;

    @Override
    public void init() {

        binding.btSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new BasePdaItemSingleSelectView.Builder<String, ItemTextBinding>(getContext())
                        .setTitle("aAAA")
                        .setSearchable(true)
                        .setDataObservable(new BasePdaItemSingleSelectView.DataObservable<String>() {
                            @Override
                            public Single<ListResult<String>> getData(String key, int page) {
                                return Single.just(new ListResult(Arrays.asList("1","2","3")));
                            }
                        })
                        .setSimpleAdapter(new BaseSimpleAdapter<String, ItemTextBinding>() {
                            @Override
                            public void setData(ItemTextBinding holder, String value, int position) {
                                super.setData(holder, value, position);
                                holder.text.setText(value);

                            }
                        })
                        .show();
            }
        });

        List<Fragment> fragments = Arrays.asList(new DemoFragment());

        binding.viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(0);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });

        new TabLayoutMediator(binding.tabLayout, binding.viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("任务信息");
                    break;

            }

        }).attach();

        getWindow().setFormat(PixelFormat.TRANSPARENT);

        binding.scanWidget.postDelayed(() -> binding.scanWidget.requestScan(), 1000);

        binding.scanWidget.setCallback(new ScanWidgetView.Callback() {
            @Override
            public void onFocusChange(boolean focus) {

            }

            @Override
            public void onData(String data) {
                Toasts.showSuccessToast(data);
            }
        });


//        ValueAnimator animator = ValueAnimator.ofFloat(0,1);
//        animator.addUpdateListener(animation -> binding.progress.setRate((Float) animation.getAnimatedValue()));
//        animator.setDuration(20000);
//        animator.setRepeatCount(-1);
//        animator.start();


        binding.button.setOnClickListener(view -> {
            new DemoFormView(getContext())
                    .builder()
                    .show();

//            new BaseItemSingleSelectView.Builder<OrderInfo, ItemOrderInfoBinding>(getContext())
//                    .setHeaderRes(elijah.base.R.layout.item_code_name_header)
//                    .setDataObservable((key, page) -> Single.just(new ListResult<>(Arrays.asList(new OrderInfo("1", 2), new OrderInfo("3", 3), new OrderInfo("5", 4)))))
//                    .setSimpleAdapter(new BaseSimpleAdapter<OrderInfo, ItemOrderInfoBinding>() {
//                    })
//                    .show();
        });


        demoFragment = new DemoFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.container, demoFragment).commit();


//
//        dataBinding.bindData(orderInfo);

        BaseAdapter<OrderInfo, ItemOrderInfoBinding> baseAdapter = new BaseSimpleAdapter<OrderInfo, ItemOrderInfoBinding>() {
        };
        binding.list.setAdapter(baseAdapter);
        binding.list.setLayoutManager(new LinearLayoutManager(getContext()));
        baseAdapter.setData(Arrays.asList(new OrderInfo("1", "2"), new OrderInfo("3", "4"), new OrderInfo("5", "6")));


    }


    public static final String PAGE_LOOP = "PAGE_LOOP";


}